package com.wframe.project.former.dataservices.upload;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.frame.common.SystemConfig;
import com.frame.database.JBaseDao;
import com.frame.database.JdataBean;
import com.frame.database.util.Seq;

public class UploadDao extends JBaseDao{

	public UploadDao(){
		super("");
	}

	public String createDataid(){
		return new Seq().getKeyId("wb_file_dataid").toString();
	}
	
	public String saveFile(FileBean bean) throws Exception{
		bean.setId(Integer.parseInt(new Seq().getKeyId("wb_file_id").toString()));
		bean.setCreatetime(new Date().getTime()/1000);
		getSession().save(bean);
		return String.valueOf(bean.getId());
	}
	public List<JdataBean> queryFiles(String userid,String dir) throws Exception{
		return getSession().query("select * from wb_files where userid=? and filedir=? and deltag=0 order by id desc", new Object[]{userid,dir});
	}
	
	public List<JdataBean> queryFiles(String dataid) throws Exception{
		return getSession().query("select * from wb_files where dataid=? and deltag=0 order by id", new Object[]{dataid});
	}
	public JdataBean getFile(String fid) throws Exception{
		return getSession().getObject("select * from wb_files where id=?", new Object[]{fid});
	}
	public void setFileDataId(String dataid,String... fileid) throws Exception{
		List<String> parms=new ArrayList<String>();
		String sql="update wb_files set dataid=? where id in (";
		for(int i=0;i<fileid.length;i++){if(i!=0)sql+=",";sql+="?";parms.add(fileid[i]);System.out.println("=fileid[i]="+fileid[i]);}
		Object[] objs=new Object[parms.size()+1];
		objs[0]=dataid;
		for(int i=0;i<parms.size();i++)objs[i+1]=parms.get(i);
		sql+=")";
		getSession().update(sql,objs);
	}
	public void clearFiles(String dataid) throws Exception{
		if(StringUtils.isNotBlank(dataid))
		getSession().update("update wb_files set deltag=1 where dataid=?", new Object[]{dataid});
	}
	/**
	 * 
	 * 功能:删除文件和清除磁盘文件<br>
	 * 参数:<br>
	 * 作者:王海锋<br>
	 * 时间:2016-3-3下午12:20:09<br>
	 * @throws Exception 
	 */
	public void delAndRemoveFiles(String dataid) throws Exception{
		List<JdataBean> list=getSession().query("select * from wb_files where dataid=?", new Object[]{dataid});
		String basepath=SystemConfig.getInstance().getRootPath();
		for(JdataBean f:list){
			try{
				File file=new File(basepath+f.getString("filepath"));
				file.delete();
			}catch(Exception e){}
		}
	}
	/**
	 * 
	 * 功能:删除文件和清除磁盘文件<br>
	 * 参数:<br>
	 * 作者:王海锋<br>
	 * 时间:2016-3-3下午12:20:09<br>
	 * @throws Exception 
	 */
	public void delAndRemoveFile(String fileid) throws Exception{
		if(StringUtils.isNotBlank(fileid)){
			JdataBean filebean=getSession().getObject("select * from wb_files where id=?", new Object[]{fileid});
			String basepath=SystemConfig.getInstance().getRootPath();
			if(filebean!=null){
				try{
					File file=new File(basepath+filebean.getString("filepath"));
					file.delete();
				}catch(Exception e){}
			}
		}
	}
	public void delAndRemoveFilesNot(String dataid,String... files) throws Exception{
		String ids="";
		if(files!=null){
			for(String fid:files){if(StringUtils.isNotBlank(fid)){if(!ids.equals(""))ids+=",";ids+=fid;}}
			if(!ids.equals(""))ids=" and id not in ("+ids+")";
		}
		List<JdataBean> list=getSession().query("select * from wb_files where dataid=?"+ids, new Object[]{dataid});
		getSession().delete("delete from wb_files where dataid=?"+ids, new Object[]{dataid});
		String basepath=SystemConfig.getInstance().getRootPath();
		for(JdataBean f:list){
			try{
				File file=new File(basepath+f.getString("filepath"));
				file.delete();
			}catch(Exception e){}
		}
	}
	
	public String addPhoto(JdataBean p) throws Exception{
		p.put("deltag", 0);
		return getSession().saveReturnKey("app_pictures",p.getDataMap());
	}

	public List<JdataBean> queryPhotos(String dataid) throws Exception{
		if(StringUtils.isBlank(dataid))return new ArrayList<JdataBean>();
		return getSession().query("select * from app_pictures where dataid=? and deltag=0 order by orderno,id", new Object[]{dataid});
	}
	public void copyPhotoId(String dataid,String... photoid) throws Exception{
		String ids="";
		for(String id:photoid){
			if(!ids.equals(""))ids+=",";
			ids+=id;
		}
		getSession().update("insert into app_pictures(filename,filetype,filepath,createtime,viewpath,dataid,deltag) select filename,filetype,filepath,createtime,viewpath,"+dataid+",0 from app_pictures where id in ("+ids+") and dataid is not null",null);
		getSession().update("update app_pictures set dataid="+dataid+" where id in ("+ids+") and dataid is null",null);
		
	}
	public void setPhotoDataId(String dataid,String... photoid) throws Exception{
		for(int i=0;i<photoid.length;i++)
			getSession().update("update app_pictures set dataid=?,orderno=? where id=?",new Object[]{dataid,i,photoid[i]});
	}
	public void setPhotoDataIdFeedBack(String dataid,String... photoid) throws Exception{
		for(int i=0;i<photoid.length;i++) {
			String s = photoid[i];
			getSession().update("update app_pictures set dataid=?,orderno=? where id=?", new Object[]{dataid, i, s});
		}
	}
	public void clearPhotosNot(String dataid,String... files) throws Exception{
		if(StringUtils.isNotBlank(dataid)){
			String ids="";
			if(files!=null){
				for(String fid:files){if(StringUtils.isNotBlank(fid)){if(!ids.equals(""))ids+=",";ids+=fid;}}
				if(!ids.equals(""))ids=" and id not in ("+ids+")";
			}
			getSession().update("delete from app_pictures where dataid=?"+ids, new Object[]{dataid});
		}
	}
	public JdataBean getPhoto(String id) throws Exception{
		return getSession().getObject("select * from app_pictures where id=?", new Object[]{id});
	}
}
